<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<style type="text/css">
#edit_goods_info .box label span {
	display: inline-block;
	width: 74px;
	text-align: right;
}

#edit_goods_info .box label {
	margin-left: 20px;
}

#edit_goods_info .formBox {
	padding: 0;
	position: relative;
}

#edit_goods_info .formBox>div {
	padding: 20px;
}

#edit_goods_info .uploadImg {
	width: 220px;
	position: absolute;
	top: 30px;
	left: 500px;
    text-align: left;
}

#edit_goods_info #goodsImagePreview {
	display: inline-block;
	width: 80px;
	height: 80px;
	background-color: #f5f5f5;
}

#edit_goods_info .updateBox {
	padding: 20px;
}

#edit_goods_info .updateBox label span {
	width: 96px;
}

#edit_goods_info .uploadFile {
    position: relative;
    display: inline-block;
    width: 34px;
    height: 20px;
    color: #2b7dbc;
    text-decoration: none;
    cursor: pointer;
}

#edit_goods_info .uploadFile input {
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    width: 100%;
    height: 100%;
    opacity: 0;
}

#edit_goods_info .red { color: #fb4a4a; font-size: 12px; }
#edit_goods_info .r { float: right; }

</style>
<div class="modal fade" id="edit_goods_info" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document" style="width: 800px;">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">编辑物品</h4>
			</div>
			<form id="edit_goods_form" class="form">
				<input type="hidden" id="inteminformationid" name="inteminformationid">
				<div class="box modal-body">
					<p>
						<label>物料编码</label>
						<input type="text" class="form-control" name="itemcode" id="itemcode" readonly="readonly"></input>
						<label class="must">中文名称</label>
						<input type="text" class="form-control" name="chinesename" id="chinesename"></input>
						<label>英文名称</label>
						<input type="text" class="form-control" name="englishname" id="englishname"></input>
					</p>
					<p>
						<label class="must">物品条码</label>
						<input type="text" class="form-control" name="articlebarcode" id="articlebarcode"></input>
						<label>中文简称</label>
						<input type="text" class="form-control" name="chineseabbreviation" id="chineseabbreviation"></input>
						<label>英文简称</label>
						<input type="text" class="form-control" name="englishabbreviation" id="englishabbreviation"></input>
					</p>
				</div>
				<div class="box formBox">
					<div>
						<p>
							<label class="must">类别</label>
							<select name="category" id="category" class="form-control select">
								<option value="">不限</option>
								<c:forEach items="${categorylist }" var="category">
									<option value="${category.categoryid}">${category.categoryname}</option>
								</c:forEach>
							</select>
							<label class="must">物品状态</label>
							<select name="itemstatus" id="itemstatus" class="form-control select">
								<option value="0">可用</option>
								<option value="1">不可用</option>
							</select>
						</p>
						<p>
							<label>销售规格</label>
							<input type="text" class="form-control" id="salesspecifications" name="salesspecifications"></input>
							<label>单位</label>
							<input type="text" class="form-control" id="salesunit" name="salesunit"></input>
						</p>
						<p>
							<label class="must">采购规格</label>
							<input type="text" class="form-control" id="purchasespecifications" name="purchasespecifications"></input>
							<label>单位</label>
							<input type="text" class="form-control" id="purchasingunit" name="purchasingunit"></input>
							<label class="must">物品类型</label>
							<select name="materialmaterial" id="materialmaterial" class="form-control select">
								<option value="IMPA物料">IMPA物料</option>
								<option value="普通物料">普通物料</option>
							</select>
						</p>
						<p>
							<label class="must">品牌</label>
							<input type="text" class="form-control" id="brand" name="brand"></input>
							<label class="must">产地</label>
							<input type="text" class="form-control" id="placeoforigin" name="placeoforigin"></input>
							<label class="must">属性</label>
							<select id="itemproperties" name="itemproperties" class="form-control select">
								<option value="可销售">可销售</option>
								<option value="不可销售">不可销售</option>
							</select>
						</p>
						<p>
							<label class="must">有效期</label>
							<input type="text" class="form-control" id="validity" name="validity" style="width:65px;"></input>
							<select name="validityunit" id="validityselect" style="width:70px;" class="form-control select">
									<option value="年">年</option>
									<option value="月">月</option>
									<option value="日">日</option>
							</select>
							<label>备注</label>
							<input type="text" class="form-control" id="description" name="description" style="width:370px;"></input>
						</p>
						<p>
							<label class="must">库存上限</label>
							<input type="text" class="form-control" id="maxstock" name="maxstock"></input>
							<label class="must">库存下限</label>
							<input type="text" class="form-control" id="minstock" name="minstock"></input>
						</p>
						<p>
							<label>当前库存</label>
							<input type="text" class="form-control" id="currentstock" name="currentstock" readonly></input>
							<label>最近进价</label>
							<input type="text" class="form-control" id="latestprice" name="latestprice"></input>
							<label>建议售价</label>
							<input type="text" class="form-control" id="proposalprice" name="proposalprice"></input>
						</p>
						<div class="uploadImg">
							<span class="r img">
							<img src="" id="goodsImagePreview">
							</span>
							<span>图片 <a class="uploadFile">上传 <input type="file" name="goodsImage" id="edit_good_image" onchange="previewEditImage(this);" accept="image/png,image/jpg,image/jpeg"></input>
								</a>
							</span><br/>
							<span class="red">图片上传要求：图片比例为1:1，图片大小不得超过500k</span>
						</div>
					</div>
				</div>
				<div class="box updateBox">
					<p>
						<label>创建时间</label>
						<input type="text" class="form-control" readonly id="createdate"></input>
						<label>最后处理时间</label>
						<input type="text" class="form-control" readonly id="lastprocessingtime"></input>
					</p>
					<p>
						<label>创建人</label>
						<input type="text" class="form-control" readonly id="createpersonname"></input>
						<label>最后处理人</label>
						<input type="text" class="form-control" readonly id="lastpersonname"></input>
					</p>
				</div>
			</form>
			<div class="red" id="edit_goods_msg" style="text-align:center;"></div>
			<div class="modal-footer">
				<c:if test="${isEdit }">
				<button type="button" class="btn btn1" onclick="saveEidtGoods();">保存</button>
				</c:if>
				<button type="button" class="btn btn2" data-dismiss="modal">取消</button>
			</div>
		</div>
	</div>
</div>
<script>

function editGoods(id){
	$('#edit_goods_info input:hidden').val("");
	$('#edit_goods_info input:text').val("");
	$("#edit_goods_info select").val("");
	$.ajax({
		url:"<%=basePath %>iteminfo/findGoodsById.do",
		type:"post",
		async : false,
		dataType:"json",
		data:"inteminformationid="+id,
		success:function(data){
			for(var key in data){
				$('#edit_goods_info #'+key).val(data[key]);
			}
			var date=new Date();
			date.setTime(data.createdate);
			$("#edit_goods_info #createdate").val(date.format('yyyy-MM-dd HH:mm:ss'));
			date.setTime(data.lastprocessingtime);
			$("#edit_goods_info #lastprocessingtime").val(date.format('yyyy-MM-dd HH:mm:ss'));
			$("#edit_goods_info #goodsImagePreview").attr("src","<%=basePath %>iteminfo/fileDownload.do?filename="+data.picture);
			$("#edit_goods_info #validity").val(data['validity'].substring(0,data['validity'].length-1));
			$("#edit_goods_info #validityselect").val(data['validity'].substring(data['validity'].length-1));
		}
	});
	$('#edit_goods_info').modal('show');
}

function saveEidtGoods(){
	if(!validateEditForm()) return;
	$.ajax({
		url:"<%=basePath %>iteminfo/update.do",
		type:"post",
		async : false,
		dataType:"json",
        processData:false,
        contentType:false,
		data : new FormData(document.getElementById("edit_goods_form")),
		success:function(data){
			if(data.result=="success"){
				$("#edit_goods_msg").html("");
				$('#edit_goods_info :text').val("");
				$("#edit_goods_info select").val("");
				$('#edit_goods_info').modal('hide');
				location.href=location.href;
			}else{
				$("#edit_goods_msg").html("修改物品信息失败：",data.error);
			}
		},
		error :function(){
			$("#edit_goods_msg").html("修改物品信息失败！")
		}
	});
}

function validateEditForm(){
 	if(!validateString("物品中文名","#edit_goods_info [name='chinesename']",30,false)){
		return false;
	}
	if(!validateString("物品英文名","#edit_goods_info [name='englishname']",30,true)){
		return false;
	}
	if(!validateString("物品条码","#edit_goods_info [name='articlebarcode']",12,false)){
		return false;
	}
	if(!validateString("中文简称","#edit_goods_info [name='chineseabbreviation']",30,true)){
		return false;
	}
	if(!validateString("英文简称","#edit_goods_info [name='englishabbreviation']",30,true)){
		return false;
	}
	if(!validateSelect("类别","#edit_goods_info [name='category']",false)){
		return false;
	}
	if(!validateSelect("物品状态","#edit_goods_info [name='itemstatus']",false)){
		return false;
	}
	if(!validateNumber("销售规格","#edit_goods_info [name='salesspecifications']",1,10000,true)){
		return false;
	}
	if(!validateString("销售单位","#edit_goods_info [name='salesunit']",30,true)){
		return false;
	}
	if(!validateNumber("采购规格","#edit_goods_info [name='purchasespecifications']",1,10000,false)){
		return false;
	}
	if(!validateString("采购单位","#edit_goods_info [name='purchasingunit']",30,false)){
		return false;
	}
	if(!validateSelect("物品类型","#edit_goods_info [name='materialmaterial']",false)){
		return false;
	}
	if(!validateString("品牌","#edit_goods_info [name='brand']",18,false)){
		return false;
	}
	if(!validateString("产地","#edit_goods_info [name='placeoforigin']",30,true)){
		return false;
	} 
	if(!validateSelect("属性","#edit_goods_info [name='itemproperties']",false)){
		return false;
	} 
	if(!validateNumber("有效期","#edit_goods_info [name='validity']",0,999,false)){
		return false;
	}
	if(!validateSelect("有效期单位","#edit_goods_info [name='validityunit']",false)){
		return false;
	}
	if(!validateString("备注","#edit_goods_info [name='description']",500,true)){
		return false;
	}
	if(!validateNumber("库存上限","#edit_goods_info [name='maxstock']",1,999999999,false)){
		return false;
	}
	if(!validateNumber("库存下限","#edit_goods_info [name='minstock']",0,999999999,false)){
		return false;
	}
	return true;
}

function previewEditImage(obj){
	var iamge=$(obj)[0];
    var src = window.URL.createObjectURL(iamge.files[0]);  
	$("#edit_goods_info #goodsImagePreview").attr("src",src);
}
</script>